package com.example.BigDecimal;

import java.math.BigDecimal;

public class Test1BigDecimal {
    public static void main(String[] args) {
        /*BigDecimal bd1 = new BigDecimal(10.00);
        BigDecimal bd2 = new BigDecimal("0.2221");

        System.out.println(bd1);
        System.out.println(bd2);*/

        // 如果想要进行精确计算，那么请使用字符串的构造
        BigDecimal b1 = new BigDecimal("0.1");
        BigDecimal b2 = new BigDecimal("0.2");
        BigDecimal add = b1.add(b2);
        System.out.println("和为：" + add); //和为：0.3

        BigDecimal subtract = b1.subtract(b2);
        System.out.println("差为：" + subtract); //差为：-0.1

        BigDecimal multiply = b1.multiply(b2);
        System.out.println("积为：" + multiply); //积为：0.02

        BigDecimal divide = b1.divide(b2);
        System.out.println("商为：" + divide); //商为：0.5

        BigDecimal bd1 = new BigDecimal("10.0");
        BigDecimal bd2 = new BigDecimal("3.0");
        BigDecimal divide1 = bd1.divide(bd2, 2, BigDecimal.ROUND_UP);    //3.34
        System.out.println(divide1);
        divide1 = bd1.divide(bd2, 2, BigDecimal.ROUND_FLOOR);    //3.33
        System.out.println(divide1);
        divide1 = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);    //3.33
        System.out.println(divide1);

    }
}
